#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <unistd.h>
#include <sys/socket.h>
#include <sys/ioctl.h>
#include <linux/if_packet.h>
#include <linux/if_ether.h>
#include <linux/if.h>
#include <arpa/inet.h>
#include <net/ethernet.h>

#include "raw_ethernet.h"

int main(int argc, char *argv[]) {
    if (argc != 3) {
        fprintf(stderr, "Usage: %s <interface> <dest_mac>\n", argv[0]);
        fprintf(stderr, "Example: %s eth0 AA:BB:CC:DD:EE:FF\n", argv[0]);
        return 1;
    }

    // 1. 创建 RAW Socket
    int sock = socket(AF_PACKET, SOCK_RAW, htons(ETH_P_ALL));
    if (sock < 0) {
        perror("socket");
        return 1;
    }

    // 2. 获取接口索引
    struct ifreq ifr;
    memset(&ifr, 0, sizeof(ifr));
    strncpy(ifr.ifr_name, argv[1], IFNAMSIZ - 1);
    if (ioctl(sock, SIOCGIFINDEX, &ifr) < 0) {
        perror("ioctl(SIOCGIFINDEX)");
        close(sock);
        return 1;
    }
	int ifindex = ifr.ifr_ifindex;

    // 3. 解析目标 MAC 地址（格式：AA:BB:CC:DD:EE:FF）
    uint8_t dest_mac[ETH_ALEN];
    if (sscanf(argv[2], "%02hhx:%02hhx:%02hhx:%02hhx:%02hhx:%02hhx",
              &dest_mac[0], &dest_mac[1], &dest_mac[2],
              &dest_mac[3], &dest_mac[4], &dest_mac[5]) != 6) {
        fprintf(stderr, "Invalid MAC address format\n");
        close(sock);
        return 1;
    }

    // 4. 获取源 MAC 地址
    if (ioctl(sock, SIOCGIFHWADDR, &ifr) < 0) {
        perror("ioctl(SIOCGIFHWADDR)");
        close(sock);
        return 1;
    }
    uint8_t src_mac[ETH_ALEN];
    memcpy(src_mac, ifr.ifr_hwaddr.sa_data, ETH_ALEN);

    // 5. 构造以太网帧（MAC头 + 自定义协议头 + 负载）
    uint8_t buffer[1024];
    memset(buffer, 0, sizeof(buffer));

    // 5.1 填充 MAC 头
    struct ethhdr *eth = (struct ethhdr *)buffer;
    memcpy(eth->h_dest, dest_mac, ETH_ALEN);   // 目标MAC
    memcpy(eth->h_source, src_mac, ETH_ALEN);  // 源MAC
    eth->h_proto = htons(CUSTOM_ETHERTYPE);    // 自定义EtherType

    // 5.2 填充自定义协议头和负载（示例：添加4字节头部和"Hello"消息）
    uint32_t *custom_header = (uint32_t *)(buffer + sizeof(struct ethhdr));
    *custom_header = htonl(0x12345678);  // 示例：自定义头部

    char *payload = (char *)(buffer + sizeof(struct ethhdr) + 4);
    strcpy(payload, "Hello from custom protocol!");

    size_t frame_len = sizeof(struct ethhdr) + 4 + strlen(payload) + 1;  // 总帧长度

    // 6. 发送帧
	struct sockaddr_ll sa = {
		.sll_family = AF_PACKET,
		.sll_ifindex = ifindex,
		.sll_halen = ETH_ALEN,
		.sll_protocol = htons(CUSTOM_ETHERTYPE),
	};
	
    memcpy(sa.sll_addr, dest_mac, ETH_ALEN);  // 目标MAC

	// 在构造 sockaddr_ll 之后添加调试输出
	printf("Interface Index: %d\n", sa.sll_ifindex);
	printf("Dest MAC: %02X:%02X:%02X:%02X:%02X:%02X\n",
		dest_mac[0], dest_mac[1], dest_mac[2],
		dest_mac[3], dest_mac[4], dest_mac[5]);


    ssize_t sent = sendto(sock, buffer, frame_len, 0, (struct sockaddr *)&sa, sizeof(sa));
    if (sent < 0) {
        perror("sendto");
    } else {
        printf("Sent %zd bytes via %s (EtherType=0x%04X)\n", sent, argv[1], CUSTOM_ETHERTYPE);
    }

    close(sock);
    return 0;
}
